﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Component;
using System.Windows.Forms;
using System.IO;
using DevExpress.Xpo;

public partial class BackEnd_NewsList : System.Web.UI.UserControl
{
    protected int i = 1;
    public int STT = 1;
    ArrayList al = new ArrayList();
    public string _Url = BackEndQuery._AdminQuery+ConstString.NewM+"&"+ConstString._Action+"="+Component.ConstString.Add;
    public bool choose = false;
    public string url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind(NewsDB.GetAllNews(true));
        grid.RowCancelingEdit += delegate {  Response.Redirect(Request.Url.ToString()); };
    }
    #region ----------------------------- Ham viet tay -------------------------------------
    void DataBind(object al)
    {
        grid.DataKeyNames = new string[] { ColumNews._Id};
        grid.DataSource = al;
        grid.DataBind();
    }
   
    #endregion
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;      
        DropDownList ddlAct = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlAct");
        System.Web.UI.WebControls.TextBox txtTitile = (System.Web.UI.WebControls.TextBox)grid.Rows[e.RowIndex].FindControl("txtTitile");
        News news = NewsDB.GetObjectByid(ConvertType.ToInt(id),true);      
        news.Active = ConvertType.ToBool(ddlAct.Text);
        news.Titile = txtTitile.Text.Trim();
        if (NewsDB.Save(news) == 1)
        {
            this.labMessage.Text=ConstString.chenTC;
        }
         Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(NewsDB.GetAllNews(true));
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            News news = NewsDB.GetObjectByid(ConvertType.ToInt(id),true);
            if (File.Exists(Server.MapPath(ConvertType.ToString(news.Img).Replace("/", "\\"))) == true)
                File.Delete(Server.MapPath(news.Img.Replace("/", "\\")));
            string path11 = ConvertType.ToString(news.Img) == "" ? "" : Server.MapPath(ConvertType.ToString(news.Img).Substring(0, ConvertType.ToString(news.Img).LastIndexOf("/")).Replace("/", "\\"));
            if (Directory.Exists(path11) == true)
            {
                if (Directory.GetFiles(path11).Length <= 0)
                    Directory.Delete(path11);
            }
            if (NewsDB.Delete(news) == 1)
            {
                this.labMessage.Text = ConstString.xoaTC;
            }            
        }
        catch { }
        DataBind(NewsDB.GetAllNews(true));
    }

    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(NewsDB.GetAllNews(true));
    }
    protected void Delete(object sender, EventArgs e)
    {
        foreach (string  key in Request.Form.AllKeys)
        {
            if (key.Contains("check")==true)
            {
                News news = Component.NewsDB.GetObjectByid(ConvertType.ToInt(key.Replace("check","")), true);
                if (news!=null)
                {
                   Component.NewsDB.Delete(news);
                }
            }
        }
        DataBind(NewsDB.GetAll(true));
    }
}
